/**
 * BBD Service Inc
 * All Rights Reserved @2018
 */
package com.magicmovie.web.controller;

import com.aliyun.oss.model.OSSObjectSummary;
import com.magicmovie.entity.Constants;
import com.magicmovie.utils.AjaxMsg;
import com.magicmovie.utils.OSSClientUtils;
import com.magicmovie.utils.OssMultipartUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 媒体中心
 *
 * @author
 */
@Controller
@RequestMapping("/media")
public class MediaController {
    @Autowired
    private OSSClientUtils ossClientUtils;
    @Autowired
    private OssMultipartUploader ossMultipartUploader;

    @RequestMapping(value = "/list")
    public String list(Model model) {
        List<OSSObjectSummary> list = ossClientUtils.getFiles(Constants.UPLOAD_MEDIA_PATH);
        model.addAttribute("list", list);
        return "media/list";
    }

    @RequestMapping(value = "/uploadImages")
    @ResponseBody
    public AjaxMsg uploadImages(MultipartFile[] file) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            List<String> urls = ossClientUtils.uploadImgs(file);
            ajaxMsg.setDatas(urls);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxMsg;
    }

//    @RequestMapping(value = "/uploadMovies")
//    @ResponseBody
//    public AjaxMsg uploadMovies(MultipartFile file) {
//        AjaxMsg ajaxMsg = new AjaxMsg();
//        try {
//            file.getInputStream()
//            String uploadId = ossMultipartUploader.upload(file);
//            ajaxMsg.setCode(AjaxMsg.SUCCESS);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return ajaxMsg;
//    }
}
